﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using UnityEngine;
using UnityEngine.Rendering;

public class ShopUI : MonoBehaviour {
    public List<GameObject> shopList = new();
    public GameObject returnCall;
    private bool[] bought = new bool[8] { false, false, false, false, false, false, false, false };
    private int[] prices = new int[8];
    private void OnDestroy() {
        foreach (var shop in shopList) {
            Destroy(shop);
        }
        Destroy(returnCall);
    }
    public void Init(ShopEvent shop) {
        for (int i = 0; i < shop.MoneyCardTuple.Count; i++) {
            GameObject capsule = GameObject.CreatePrimitive(PrimitiveType.Capsule);
            capsule.transform.position = new Vector3(((i / 2) - 2f) * 3f, (1 - (i % 2)) * 3f);
            capsule.transform.localScale = new Vector3(1.6f, 1.0f, 1.0f);
            GameObject description = new();
            description.transform.position = new Vector3(0, 0);
            TextMesh mesh_description = description.AddComponent<TextMesh>();
            mesh_description.text = shop.MoneyCardTuple[i].Item2.description;
            mesh_description.color = Color.black;
            mesh_description.characterSize = 0.1f;
            mesh_description.anchor = TextAnchor.MiddleCenter;
            description.transform.SetParent(capsule.transform, false);
            GameObject cost = new();
            cost.transform.position = new Vector3(-0.4f, 0.6f, 0);
            TextMesh mesh_cost = cost.AddComponent<TextMesh>();
            mesh_cost.text = $"{shop.MoneyCardTuple[i].Item1}";
            mesh_cost.color = Color.black;
            mesh_cost.characterSize = 0.1f;
            mesh_cost.anchor = TextAnchor.MiddleCenter;
            cost.transform.SetParent(capsule.transform, false);
            shopList.Add(capsule);
            prices[i] = shop.MoneyCardTuple[i].Item1;
        }
        returnCall = GameObject.CreatePrimitive(PrimitiveType.Capsule);
        returnCall.transform.position = new Vector3(-7f, 4f);
        returnCall.transform.localScale = new Vector3(0.5f, 0.1f);
    }
    public IEnumerator SelectGood(Action<int> callback) {
        GameObject go_coin = new();
        go_coin.transform.position = new Vector3(0, 4.8f, 0);
        TextMesh mesh_coin = go_coin.AddComponent<TextMesh>();
        mesh_coin.text = $"Coin : {GameManager.Instance.MoneyHold}";
        mesh_coin.color = Color.black;
        mesh_coin.characterSize = 0.5f;
        while(true) {
            yield return null;
            if (Input.GetMouseButtonDown(0)) {
                Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
                if (Physics.Raycast(ray, out RaycastHit hit)) {
                    if (shopList.Contains(hit.collider.gameObject)) {
                        int idx = shopList.IndexOf(hit.collider.gameObject);
                        if (bought[idx]) {
                            continue;
                        }
                        if (GameManager.Instance.MoneyHold < prices[idx]) {
                            continue;
                        }
                        bought[idx] = true;
                        Destroy(hit.collider.gameObject);
                        callback(idx + 1);
                        Destroy(go_coin);
                        yield break;
                    } else if(hit.collider.gameObject == returnCall) {
                        callback(-1);
                        yield break;
                    }
                }
            }
        }
    }
}